package com.appspot.battlerafts.servlets;

import com.appspot.battlerafts.classes.Game;
import com.appspot.battlerafts.classes.User;
import com.appspot.battlerafts.enums.GameState;
import com.appspot.battlerafts.jsons.ChannelDataJSON;
import com.appspot.battlerafts.jsons.PusherAuthResponseJSON;
import com.appspot.battlerafts.jsons.UserInfoJSON;
import com.appspot.battlerafts.utils.PMF;
import com.appspot.battlerafts.utils.Pusher;
import com.google.gson.Gson;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Logger;

import static com.appspot.battlerafts.utils.Pusher.hmacsha256Representation;
import static com.appspot.battlerafts.utils.Util.unicodeEscape;

/**
 * Created with IntelliJ IDEA.
 * User: Tonis
 * Date: 07.04.12
 * Time: 17:56
 * To change this template use File | Settings | File Templates.
 */
public class PusherAuthenticate extends HttpServlet {
    private static final Logger log = Logger.getLogger(PusherAuthenticate.class.getName());

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String channel_name = request.getParameter("channel_name");
        String socket_id = request.getParameter("socket_id");

        PersistenceManager pm = PMF.get().getPersistenceManager();
        List<User> userByCurrentSocket = getUserByCurrentSocket(socket_id, pm);
        log.info("QUERY RESULTS:" + userByCurrentSocket.toString());

        List<String> gamesWhereIniator = getGamesWhereInitator(pm, userByCurrentSocket);

        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = response.getWriter();
        Gson gson = new Gson();

        PusherAuthResponseJSON pusherAuthResponseJSON = new PusherAuthResponseJSON();
        ChannelDataJSON channelDataJSON = new ChannelDataJSON();
        UserInfoJSON userInfoJSON = new UserInfoJSON();

        userInfoJSON.name = unicodeEscape(userByCurrentSocket.get(0).getName());
        userInfoJSON.isInitator = gamesWhereIniator.size() == 0 ? false : true;

        channelDataJSON.user_id = userByCurrentSocket.get(0).getUID().toString();
        channelDataJSON.user_info = userInfoJSON;

        String string_to_sign = socket_id + ":" + channel_name + ":" + gson.toJson(channelDataJSON);
        String signature = hmacsha256Representation(string_to_sign);

        pusherAuthResponseJSON.auth = Pusher.pusherApplicationKey+":"+signature;
        pusherAuthResponseJSON.channel_data = gson.toJson(channelDataJSON);

        writer.write(gson.toJson(pusherAuthResponseJSON));
        return;
    }

    private List<String> getGamesWhereInitator(PersistenceManager pm, List<User> results) {
        Query q = pm.newQuery("select gameID from " + Game.class.getName());
        q.setFilter("gameState == '" + GameState.WAITING.toString() + "' && initator == id");
        q.declareParameters("Long id");
        return (List<String>) q.execute(results.get(0).getUID());
    }

    private List<User> getUserByCurrentSocket(String socket_id, PersistenceManager pm) {
        Query q = pm.newQuery(User.class);
        q.setFilter("currentSocketID == '" + socket_id+ "'");
        return (List<User>) q.execute();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
